package com.tianzj.boot.mvc.controller.system;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.tianzj.boot.mvc.entity.system.SysUser;
import com.tianzj.boot.mvc.service.system.SysUserService;
import com.tianzj.boot.util.AjaxJson;
import com.tianzj.boot.util.JsonUtil;
import com.tianzj.boot.util.MD5Utils;
import com.tianzj.boot.util.PageVo.PageVo;

import lombok.extern.slf4j.Slf4j;

@RestController()
@RequestMapping("sysUser")
@Slf4j
public class SysUserController {

	@Autowired
	private SysUserService sysUserService;
	
	/**
	 * 跳转到用户列表页面
	 * @return
	 */
	@RequestMapping("userList")
	public ModelAndView userList(){
		return new ModelAndView("user/userList");
	}

	/**
	 * 获取用户列表信息
	 * @param pageVo
	 * @return
	 */
	@RequestMapping("getUserList")
	public String getSysUserList(PageVo pageVo,SysUser sysUser){
		Pageable pageable=new PageRequest(pageVo.getPageNumber()-1,pageVo.getPageSize());
		Page<SysUser> userList = sysUserService.getSysUserList(sysUser,pageable);
		return JsonUtil.list2BootStrapJson(userList);
	}
	
	/**
	 * 添加用户信息
	 * @param sysUser
	 * @return
	 */
	@PostMapping("addSysUser")
	public ModelAndView addSysUser(SysUser sysUser){
		try {
			sysUser.setCreateDate(System.currentTimeMillis());
			sysUser.setStatus(1);
			sysUser.setPassword(MD5Utils.MD5(sysUser.getPassword()));
			sysUserService.addUser(sysUser);
		} catch (Exception e) {
			log.error(e.getMessage());
		}						
		return new ModelAndView("redirect:/sysUser/userList");
	}
	/**
	 * 编辑用户信息
	 * @param sysUser
	 * @return
	 */
	@PostMapping("editSysUser")
	public ModelAndView editSysUser(SysUser sysUser){
		try {
			sysUser.setUpdateDate(System.currentTimeMillis());
			sysUser.setPassword(MD5Utils.MD5(sysUser.getPassword()));
			sysUserService.updateUser(sysUser);
		} catch (Exception e) {
			log.error(e.getMessage());
		}						
		return new ModelAndView("redirect:/sysUser/userList");
	}
	
	@PostMapping("getSysUserById")
	public SysUser getSysUserById(SysUser sysUser){
		return sysUserService.getSysUserById(sysUser.getId());
	}
	
	@PostMapping("removeById")
	public AjaxJson removeById(String ids){
		AjaxJson json=new AjaxJson();
		try {
			json.setMsg("删除成功");
			sysUserService.deleteSysUserById(ids);
		} catch (Exception e) {
			log.error(e.getMessage());
			json.setMsg("删除失败");
		}
		return json;
	}
	
	public static void main(String[] args) {
		System.out.println(System.currentTimeMillis());
	}
}
